package employee

import (
	"fmt"
	"goskeleton/app/global/consts"
	"goskeleton/app/service/employee/curd"
	"goskeleton/app/utils/response"
	

	
	"github.com/gin-gonic/gin"
)

type Employee struct {
}

func (e *Employee) List(context *gin.Context) {
	page := context.GetFloat64(consts.ValidatorPrefix + "page")
	limit := context.GetFloat64(consts.ValidatorPrefix + "limit")
	name := context.GetString(consts.ValidatorPrefix + "name")
	workStatus := context.GetFloat64(consts.ValidatorPrefix + "work_status")
	contractType := context.GetFloat64(consts.ValidatorPrefix + "contract_type")
	hireStartDate := context.GetString(consts.ValidatorPrefix + "hire_start_date")
	hireEndDate := context.GetString(consts.ValidatorPrefix + "hire_end_date")
	departmentId := context.GetFloat64(consts.ValidatorPrefix + "department_id")
	positionId := context.GetFloat64(consts.ValidatorPrefix + "position_id")

	orderBy := "id asc"
	limitStart := (page - 1) * limit
	
	totalCounts, showList := curd.
							CreateEmployeeCurdFactory().
							ListData(
								int(limitStart), 
								int(limit), 
								orderBy, 
								name, 
								int(workStatus), 
								int(contractType), 
								hireStartDate, 
								hireEndDate,	
								int(departmentId),
								int(positionId),
							)

	if totalCounts > 0 && showList != nil {
		response.Success(context, consts.CurdStatusOkMsg, gin.H{"count": totalCounts, "data": showList})
	} else {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

func (e *Employee) Detail(context *gin.Context) {
	employeeId := context.GetFloat64(consts.ValidatorPrefix + "id")
	detail := curd.CreateEmployeeCurdFactory().ShowOneItem(int(employeeId))

	if detail == nil {
		fmt.Printf("获取员工信息失败:%v",employeeId)
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, detail)
	}
}

func (e *Employee) Create(context *gin.Context) {
	if curd.CreateEmployeeCurdFactory().InsertData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, "")
	}
}

func (e *Employee) Edit(context *gin.Context) {
	if curd.CreateEmployeeCurdFactory().UpdateData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

func (e *Employee) TotalStatistics(context *gin.Context) {
	totalStatusStatistics := curd.CreateEmployeeCurdFactory().GetTotalStatistcis()
	if totalStatusStatistics == nil {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, totalStatusStatistics)
	}
}

func (e *Employee) GetAllIdNameByWhere(context *gin.Context) {
	positionId := context.GetFloat64(consts.ValidatorPrefix + "position_id")
	departmentId := context.GetFloat64(consts.ValidatorPrefix + "department_id")
	totalCounts, tmp := curd.CreateEmployeeCurdFactory().GetAllIdNameByWhere(int(departmentId), int(positionId))
	// if totalCounts > 0 {
	response.Success(context, consts.CurdStatusOkMsg, gin.H{"count": totalCounts, "data": tmp})
	// } else {
		// response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	// }
}

func (e *Employee) Delete(context *gin.Context) {
	employeeId := context.GetFloat64(consts.ValidatorPrefix + "id")
	if curd.CreateEmployeeCurdFactory().DeleteData(int(employeeId)) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg, "")
	}
}
